import InfoBarList from '../InfoBarList/InfoBarList';

export default class WindHandler {
    constructor() {
        this.heatmapChartId = null;
        this.heatmapData = null;
        this.windVelocity = null;
        this.windVelocityData = [];
        this.windVelocityData1 = [];
        this.useData = 0;
        this.chart = null;
        this.infoObj = null;
        // this.legendObj = {
        //     name: '风力',
        //     arrName: ['0-10', '10-20.8', '20.8-28.5', '>28.5'],
        //     arrColor: ['#28AD86', '#E0B62C', '#FE4B3C', '#8E71FF']
        // };
        this.legendObj = null;
    }

    loadData(map, isInitChart) {
        let that = this;
        minemap.util.getJSON('//dev.minedata.cn/dataweather/weathertop10/wins', function (error, data) {
            const dataArr = data ? data.data.rows[0].features || [] : [];
            that.infoObj = {
                "color1": "#00C0CE",
                "color2": "#FEC819",
                "unit": "风力(级)",
                "array": [],
                // callback: (coordinate) => {
                //     map.flyTo({
                //         center: coordinate,
                //         zoom: 15
                //     });
                // }
            };
            dataArr.map((item) => {
                that.infoObj.array.push({
                    "name": item.properties.name_zh,
                    "value": item.properties.wins,
                    "coordinate": item.geometry.coordinates[Math.ceil(item.geometry.coordinates.length / 2)]
                })
            });
            if (isInitChart) {
                that.updateChart();
            }
        });
    }

    updateChart() {
        this.chart = new InfoBarList('road-weather-chart', this.infoObj || {});
    }

    loadHeatmapData(map) {
        let that = this;
        minemap.util.getJSON('../data/wins.json', function (error, data) {
            let points = data ? data.data.rows[0].wind : [];
            that.heatmapData = points;
            let series = [];
            let option = {
                GLMap: {
                    roam: true
                },
                coordinateSystem: 'GLMap',
                title: {
                    text: '风力',
                    subtext: '',
                    left: 'center',
                    textStyle: {
                        color: '#fff'
                    }
                },
                tooltip: {
                    trigger: 'item'
                },
                visualMap: {
                    show: false,
                    top: 'top',
                    min: 0,
                    max: 10,
                    seriesIndex: 0,
                    calculable: true,
                    inRange: {
                        color: ['#28AD86', '#E0B62C', '#FE4B3C', '#8E71FF']
                    }
                },
                series: [{
                    type: 'heatmap',
                    coordinateSystem: 'GLMap',
                    data: points,
                    pointSize: 14,
                    blurSize: 8
                }]
            };
            let echartslayer = minemap.Template.create({map: map, type: 'heatmap'});
            echartslayer.chart.setOption(option);
            that.heatmapChartId = echartslayer.getChartId();
            document.getElementById(that.heatmapChartId).style.display = 'none';
        });
    }

    getHeatMapChartId() {
        return this.heatmapChartId;
    }

    initWindVelocity(map, isInit) {
        Velocity.initVelocityComponent(map);
        this.windVelocity = new Velocity({
            canvas: document.getElementById('windcanvas'),
            displayValues: true,
            displayOptions: {
                velocityType: 'Velocity',
                position: 'bottomleft',
                emptyString: 'No velocity data'
            },
            uLineRatio: 2.4,
            vLineRatio: 1.2,
            maxVelocity: 10, // used to align color scale
            colorScale: null,
            data: this.windVelocityData,
            map: map
        });
        if (isInit) {
            this.loadWindVelocityData();
        }
    }

    loadWindVelocityData() {
        let that = this;
        minemap.util.getJSON('../data/wind_data_20171125.json', function (error, data) {
            that.windVelocityData = data || [];
            that.windVelocity.setData(data);
            that.windVelocity.drawLayer();
        });
        minemap.util.getJSON('../data/wind_data_20171127.json', function (error, data) {
            that.windVelocityData1 = data || [];
        });
    }

    getWindVelocityData() {
        return this.windVelocityData;
    }

    getWindVelocity() {
        return this.windVelocity;
    }

    onWindVelocityDestory() {
        if (this.windVelocity) {
            this.windVelocity.destory();
        }
    }

    onWindVelocityStart() {
        if (this.windVelocity) {
            this.windVelocity.start();
        }
    }

    onWindVelocityStop() {
        if (this.windVelocity) {
            this.windVelocity.stop();
        }
    }

    onWindVelocityReRender() {
        if (this.windVelocity) {
            this.windVelocity.drawLayer();
        }
    }

    getInfoObj() {
        return this.infoObj;
    }

    getChart() {
        return this.chart;
    }

    getLegendObj() {
        return this.legendObj;
    }

    setValue(map, value) {

    }

    setValueNum(map, valueNum) {
        if (valueNum > 6 && valueNum < 18) {
            if (this.useData !== 1) {
                this.useData = 1;
                this.windVelocity.setData(this.windVelocityData1);
            }
        } else {
            if (this.useData !== 0) {
                this.useData = 0;
                this.windVelocity.setData(this.windVelocityData);
            }

        }
    }
};